package com.trkj.service.wdm;

import java.sql.Timestamp;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;
import com.trkj.dao.wdm.ContractAdministrationDao;
import com.trkj.dao.wdm.ContractCategoryDAO;
import com.trkj.dao.wdm.StaffInformationDAO;
import com.trkj.mybatisdao.ContractAdministrationMapper;
import com.trkj.pojo.ContractAdministration;
import com.trkj.pojo.ContractCategory;
import com.trkj.pojo.StaffInformation;


@Service
@Transactional(rollbackFor=Exception.class)
public class ContractAdministrationService {
	@Autowired
	private ContractAdministrationDao  conDao;
	@Autowired
	private ContractAdministrationMapper conMapper;
	@Autowired
	private ContractCategoryDAO cDao;
	@Autowired
	private StaffInformationDAO sDao;
	
	/**
	 * 分页查询所有合同
	 * @param page
	 * @param rows
	 * @return
	 */
	public JSONArray findAllContract(){
		return conMapper.findAll();
	}
	
	/**
	 * 查询单条合同类型信息
	 * @param id
	 * @return
	 */
	public ContractCategory findOneContract(Long id){
		return cDao.findOne(id);
	}
	
	/**
	 * 新增员工合同信息
	 * @param id	员工id
	 * @param id2 	合同id
	 * @param t		开始时间
	 * @param t2	结束时间
	 * @param t3	合同期限
	 * @param t4	试用结束时间
	 * @param remarks 内容
	 * @param beizhu	备注
	 */
	public void insertContractAdmin(Long id,Long id2,Timestamp t,Timestamp t2,Timestamp t3,Timestamp t4,String remarks,String beizhu){
		ContractAdministration ct = new ContractAdministration();
		StaffInformation s = sDao.findOne(id);
		ct.setStaffInformation(s);
		
		
		ContractCategory con = cDao.findOne(id2);
		ct.setContractCategory(con);
		
		ct.setConadStarttime(t);
		ct.setConadTerm(t2);
		ct.setConadEndtime(t3);
		ct.setConadTryoutendtime(t4);
		ct.setConadContent(remarks);
		ct.setConadRemarks(beizhu);
		
		conDao.save(ct);
	}
	
	/**
	 * 分页查询所有合同
	 * @param page
	 * @param rows
	 * @return
	 */
	public JSONArray findLikeAllContract(String name){
		return conMapper.findLikeAll("%"+name+"%");
	}
	
	/**
	 * 删除合同信息
	 * @param id
	 */
	public void deleteContract(Long id){
		conDao.delete(id);
	}
	
	/**
	 * 根据编号查询合同信息做编辑
	 * @param id
	 * @return
	 */
	public ContractAdministration findOneContractAdministration(Long id){
		return conDao.findOne(id);
	}
	
	/**
	 * 根据编号修改员工合同信息
	 * @param id
	 */
	public void updateContractAdministration(Long id,Long id1,Long id2,Timestamp t,Timestamp t2,Timestamp t3,Timestamp t4,String remarks,String beizhu){
		ContractAdministration ct = conDao.findOne(id);
		StaffInformation s = sDao.findOne(id1);
		ct.setStaffInformation(s);
		
		ContractCategory con = cDao.findOne(id2);
		ct.setContractCategory(con);
		
		ct.setConadStarttime(t);
		ct.setConadTerm(t2);
		ct.setConadEndtime(t3);
		ct.setConadTryoutendtime(t4);
		ct.setConadContent(remarks);
		ct.setConadRemarks(beizhu);
		
		conDao.save(ct);
	}
}
